package com.ycxy.sh.transaction.controller;

import com.ycxy.sh.transaction.dao.vo.LayuiPageVo;
import com.ycxy.sh.transaction.service.CommoditySoldrecordService;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.dao.entity.SoldRecord;
import com.ycxy.sh.transaction.service.impl.CommoditySoldrecordServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  销售记录控制器
 * </p>
 *
 * @author hlt
 * @since 2019-12-21
 */
@Slf4j
@Api(tags = "10.商品售卖状态管理模块")
@Controller
public class SoldrecordController {
    @Autowired
    private CommoditySoldrecordService soldrecordService;

    /**
     * 删除售出记录
     * 1.前端传入需删除记录的id（id）
     * 2.判断是否是本人
     * */
    @ApiOperation("根据id删除售出记录")
    @ResponseBody
    @PostMapping("/soldrecord/delect/{id}")
    public JsonResult<Void> delectSoldRecord (@PathVariable("id") Long id) {
        log.debug("开始处理根据售出记录id删除售出记录...id为：{}",id);
        soldrecordService.deleteSoldRecord(id);
        return JsonResult.ok();
    }

    /**
     * 分页查看用户所有售出记录
     * 1.前端传入页码、分页数量
     * 2.查询分页数据
     */
    @ApiOperation("分页查看用户所有售出记录")
    @ResponseBody
    @GetMapping("/soldrecord/lookuser")
    public LayuiPageVo selectUserSoldRecord(int limit, int page, HttpSession session) {
        Long userid = (Long) session.getAttribute("userid");
        List<SoldRecord> soldRecordList = soldrecordService.pageSoldrecord((page - 1) * limit, limit, userid);
        Integer dataNumber = soldrecordService.selectSoldCount(userid);
        return new LayuiPageVo("",0,dataNumber, soldRecordList);
    }

    /**
     * 分页查看全部的售出记录
     * 1.前端传入页码、分页数量
     * 2.查询分页数据
     */
    @ApiOperation("分页查看全部的售出记录")
    @ResponseBody
    @GetMapping("/soldrecord/queryall")
    public LayuiPageVo queryAllSoldRecord(int limit, int page) {
        List<SoldRecord> soldRecordList = soldrecordService.pageSoldrecord((page - 1) * limit, limit, null);
        Integer dataNumber = soldrecordService.selectSoldCount(null);
        return new LayuiPageVo("",0,dataNumber, soldRecordList);
    }

}

